/**

 * Copyright (c) 2014 Citics Inc., All Rights Reserved.
 */
package com.apexsoft.front.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @author 022959
 *
 */
public class Md5Util {

	private static final Logger LOG = LogManager.getRootLogger();
	
    /**
     * 根据特殊规则加密
     * 
     * @param st
     * @return
     */
	private Md5Util(){
		
	}
    public static String encryptionByRules(String password) {
        
        
    	StringBuilder sb = new StringBuilder(password);
        for (int i = 0; i < 32-password.length(); i++) {
            
            sb.append((i*password.length()+password.length())%10);
        }
        
        return encryption(sb.toString()+getAscTotal(password));
    }
    
    /**
     * 
     * @param plainText
     *            明文
     * @return 32位密文
     */
    public static String encryption(String plainText) {
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuilder buf = new StringBuilder("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            re_md5 = buf.toString();

        } catch (NoSuchAlgorithmException e) {
        	LOG.error(e.getMessage(), e);
        }
        return re_md5;
    }

    /**
     * 字符转ASC
     * 
     * @param st
     * @return
     */
    public static int getAscTotal(String st) {
        
        
        int ascNumTotal = 0;
        byte[] gc = st.getBytes();
        for (int i = 0; i < gc.length; i++) {
            ascNumTotal += (int) gc[i];
        }
        return ascNumTotal;
    }
    
    private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    private static String getMD5(byte[] source)
    {
      String s = null;
      try {
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(source);
        byte[] tmp = md.digest();

        char[] str = new char[32];

        int k = 0;
        for (int i = 0; i < 16; ++i)
        {
          byte byte0 = tmp[i];
          str[k++] = hexDigits[byte0 >>> 4 & 0xF];

          str[k++] = hexDigits[byte0 & 0xF];
        }
        s = new String(str);
      }
      catch (Exception e) {
    	  LOG.error(e.getMessage(), e);
      }
      return s;
    }

    public static String getMD5ofStr(String inbuf)
    {
      return getMD5(inbuf.getBytes());
    }

    public static long b2iu(byte b)
    {
      return b;
    }

    public static String byteHEX(byte ib)
    {
      char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

      char[] ob = new char[2];
      ob[0] = Digit[(ib >>> 4 & 0xF)];
      ob[1] = Digit[(ib & 0xF)];
      String s = new String(ob);
      return s;
    }
}
